//
//  GGContractUIViewController.m
//  ggtool
//
//  Created by engfei on 2017/5/4.
//  Copyright © 2017年 GaiGai. All rights reserved.
//

#import "GGContactUIViewController.h"
// NS_CLASS_AVAILABLE_IOS(9_0)
#import <ContactsUI/ContactsUI.h>

@interface GGContactUIViewController ()<CNContactPickerDelegate, CNContactViewControllerDelegate>


@end

@implementation GGContactUIViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    // CNContactPickerViewController 允许用户单选或者多选联系人或者属性.
    CNContactPickerViewController *pickerVC = [[CNContactPickerViewController alloc] init];
    pickerVC.delegate = self;
    [self presentViewController:pickerVC animated:YES completion:nil];
    
    // 展示联系人
    //    CNContactViewController *vc = [[CNContactViewController alloc] init];
    
    
}

#pragma mark - CNContactPickerDelegate

// 当一个联系人或者属性被选中时,picker控制器会自动dismissed掉,然后回调该方法
- (void)contactPickerDidCancel:(CNContactPickerViewController *)picker {
    TFFunc;
}

// 选中单一的联系人或者属性时回调下面两个方法
// 多选的代理方法实现时,单选会被忽略
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(CNContact *)contact {
    TFFunc;
    TFLog(@"%@", contact);
    
    CNContactViewController *vc = [CNContactViewController viewControllerForContact:contact];
    vc.delegate = self;
    [self presentViewController:vc animated:YES completion:nil];
    
}
- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperty:(CNContactProperty *)contactProperty {
    TFLog(@"%@", contactProperty);
}

////  多选时会调用下面的方法
//- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContacts:(NSArray<CNContact*> *)contacts {
//    TFLog(@"%@", contacts);
//}
//- (void)contactPicker:(CNContactPickerViewController *)picker didSelectContactProperties:(NSArray<CNContactProperty*> *)contactProperties {
//    TFLog(@"%@", contactProperties);
//}


#pragma mark - CNContactViewControllerDelegate

/*!
 * @abstract Called when the user selects a single property.
 * @discussion Return NO if you do not want anything to be done or if you are handling the actions yourself.
 * Return YES if you want the default action performed for the property otherwise return NO.
 */
// 当用户选择单一的属性时会回调
// 返回YES,可以选择,返回NO,选择被禁用.
- (BOOL)contactViewController:(CNContactViewController *)viewController shouldPerformDefaultActionForContactProperty:(CNContactProperty *)property {
    return YES;
}

/*!
 * @abstract Called when the view has completed.
 * @discussion If creating a new contact, the new contact added to the contacts list will be passed.
 * If adding to an existing contact, the existing contact will be passed.
 * @note It is up to the delegate to dismiss the view controller.
 */
//
- (void)contactViewController:(CNContactViewController *)viewController didCompleteWithContact:(nullable CNContact *)contact {
    
    
}



@end
